package com.imooc.miaosha.service.impl;

import com.google.common.collect.Lists;
import com.imooc.miaosha.dao.MiaoshaOrderMapper;
import com.imooc.miaosha.dao.UserMapper;
import com.imooc.miaosha.dto.User.RegisterDTO;
import com.imooc.miaosha.dto.UserDTO;
import com.imooc.miaosha.pojo.User;
import com.imooc.miaosha.service.UserService;
import com.imooc.miaosha.util.*;


import com.imooc.miaosha.validator.ValidatorUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import javax.swing.plaf.PanelUI;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    MiaoshaOrderMapper miaoshaOrderMapper;

    @Override
    public UserDTO login(String password, String mobile,String salt) {
        log.info("=======password======={}", Md5Util.passwordToDB(password, salt));
        String digestPwd = this._getDisgtPaswword(password, salt);
        User user = userMapper.getUserByPasswordAndMobile(digestPwd, mobile);
        if (user == null) {
            return null;
        }
        UserDTO userDTO = this._createUserDTO();
        user = this._processLoginUserData(user);
        userMapper.updateByPrimaryKeySelective(user);
        BeanUtils.copyProperties(user, userDTO);
        return userDTO;
    }

    public boolean checkIsAdmin(UserDTO userDTO) {
        return userDTO.getRole() == 1;
    }

    @Override
    public JsonData listUser() {
        List<User> userList = userMapper.getAllUser();
        List<UserDTO> userDTOList = Lists.newArrayList();
        userList.forEach(user -> userDTOList.add(userDtoAdapter(user)));

        return JsonData.success(userDTOList);
    }

    @Override
    public int registerUser(RegisterDTO registerDTO) {
        User user = this._initNewUserData(registerDTO);

        return userMapper.insert(user);
    }

    private UserDTO userDtoAdapter(User user) {
        UserDTO userDTO = this._createUserDTO();
        BeanUtils.copyProperties(user, userDTO);
        userDTO.setLastLoginDate(TimeUtil.formatDate(user.getLastLoginDate()));
        userDTO.setMiaoshaCount(miaoshaOrderMapper.getOrderCountByUserId(user.getId()));
        return userDTO;
    }

    public String userExistAndReturnSalt(String mobile) {
        return userMapper.getSaltByMobile(mobile);

    }

    //初始化注册user
    public User _initNewUserData(RegisterDTO registerDTO) {
        User user = this._createUser();
        user.setLoginCount(0);
        user.setNickname(registerDTO.getNickName());
        String salt = UUIDUtil.UUID();
        user.setPassword(this._getDisgtPaswword(registerDTO.getPassword(), salt));
        user.setMobile(registerDTO.getMobile());
        user.setSalt(salt);
        return user;
    }

    //处理登录成功后的数据
    public User _processLoginUserData(User user) {

        user.setLoginCount(this._increaseLoginCount(user));
        user.setLastLoginDate(new Date());
        return user;
    }

    public int _increaseLoginCount(User user) {
        return user.getLoginCount() + 1;
    }

    public User _createUser() {
        return new User();
    }

    public UserDTO _createUserDTO() {
        return new UserDTO();
    }

    public String _getDisgtPaswword(String pwd, String salt) {
        return Md5Util.passwordToDB(pwd, salt);
    }
}
